package com.googlecode.olabs.gui.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.googlecode.olabs.gui.client.resources.Card;
import com.googlecode.olabs.gui.client.rest.ResourceObserver;
import com.googlecode.olabs.gui.client.rest.RestResource;

public class CardDisplay extends Composite implements ResourceObserver {

	private Label m_label;
	private Card m_card;

	public CardDisplay() {
		m_label = new Label("No card");
		initWidget(m_label);
	}

	public void setCard(Card card) {
		if (m_card != card) {
			if (m_card != null) {
				m_card.removeResourceObserver(this);
			}
			m_card = card;
			if (card == null) {
				m_label.setText("");
			} else {
				m_card.addResourceObserver(this);
				resourceChanged(card);
			}
		}
	}

	public void resourceChanged(RestResource resource) {
		if (m_card != resource) {
			resource.removeResourceObserver(this);
			return;
		}

		if (m_card != null) {
			m_label.setText(m_card.getTitle());
		}
	}
}
